//
// Created by guoshunw on 24-2-27.
//

#include "message_fields_with_type.h"

namespace davinci::edr {

std::string FieldWithType::toString() const {
  return std::visit(
      [this](auto &&value) -> std::string {
        using T = std::decay_t<decltype(value)>;
        if constexpr (std::is_same_v<T, std::vector<double>>) {
          std::stringstream ss;
          ss << "[";
          bool isFirst = true;
          for (auto &v : value) {
            if (!isFirst) ss << ",";
            ss << v;
            isFirst = false;
          }
          ss << "]";
          return ss.str();
        }
        if constexpr (std::is_same_v<T, double>) {
          return std::to_string(value);
        }

        if constexpr (std::is_same_v<T, std::string>) {
          return value;
        }
        return "type: " + std::to_string(type_) + ", value: unknown";
      },
      value_);
}
std::string MessageFieldsWithType::toDebugString(bool isCompact) const {
  std::stringstream ss;
  ss << "MsgFieldsWithType@" << this << ": {";
  bool isFirst = true;
  const static std::string indent = "  ";
  for (const auto &kv : fields_) {
    if (!isFirst) ss << ",";
    if (!isCompact) {
      ss << std::endl << indent;
    }
    ss << kv.first << "=" << kv.second;
    isFirst = false;
  }
  if (!isCompact) ss << std::endl;
  ss << "}";
  return ss.str();
}

FieldWithType MessageFieldsWithType::getField(
    const std::string &key, FieldWithType default_value) const {
  auto it = fields_.find(key);
  if (it == fields_.end()) {
    LOGW << "key not found: " << key << "in container " << this
         << ", return default value: " << default_value;
    return default_value;
  }
  return fields_.at(key);
}

}  // namespace davinci::edr
